package cn.com.controller.operationController;


import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Deck;
import cn.com.db.model.StoreDeck;
import cn.com.db.service.DeckService;
import cn.com.db.service.StoreDeckService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Auth(UserAuth.SELLER)
@RestController
@RequestMapping("/operation/deck")
public class DeckOperationController {

    @Autowired
    DeckService deckService;
    @Autowired
    StoreDeckService storeDeckService;

    @RequestMapping(method = RequestMethod.POST)
    public String getDeck(@RequestParam(value = "parent") String parent,
                          @RequestParam("storeId")String storeId){
        DataResult dataResult=new DataResult();
        dataResult.setData(storeDeckService.getStoreDeck(storeId,parent));
        dataResult.setTotal(storeDeckService.countStoreDeck(storeId,parent));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(value = "/onDeck",method = RequestMethod.POST)
    public String onDeck(@RequestParam("storeId") String storeId,
                         @RequestParam("deckId")String deckId,
                         @RequestParam("price")BigDecimal price){
        DataResult dataResult=new DataResult();
        if (!deckService.isExist(deckId)){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("装饰不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreDeck storeDeck=storeDeckService.getStoreDeckOfStore(storeId,deckId);
        Deck deck=deckService.getDeckById(deckId);
        if (storeDeck==null){
            storeDeck=new StoreDeck();
            storeDeck.setId(UidUtil.createUUID());
            storeDeck.setDeck(deck);
            storeDeck.setStore(storeId);
            storeDeck.setDel(false);
            storeDeck.setPrice(price);
            storeDeck.setParent(deck.getParent());
            storeDeck.setCreated_at(TimeUtil.getNowTimestamp());
            storeDeck.setUpdated_at(TimeUtil.getNowTimestamp());
            storeDeckService.onDeck(storeDeck);
            dataResult.setData(storeDeckService.getStoreDeckById(storeDeck.getId()));
        }else if (storeDeck.getDel()){
            storeDeck.setDel(false);
            storeDeck.setPrice(price);
            storeDeckService.updateDeck(storeDeck);
            dataResult.setData(storeDeckService.getStoreDeckById(storeDeck.getId()));
        }else {
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("店铺已启用该装饰");
            return JsonUtil.toJsonString(dataResult);
        }
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("操作成功");
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(value = "/offDeck",method = RequestMethod.POST)
    public String offDeck(@RequestParam("id") String storeDeckId){
        DataResult dataResult=new DataResult();
        if (!storeDeckService.isAlive(storeDeckId)){
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("装饰不存在,或装饰不处于启用状态");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreDeck storeDeck= storeDeckService.getStoreDeckById(storeDeckId);
        storeDeck.setDel(true);
        storeDeckService.updateDeck(storeDeck);
        dataResult.setData(storeDeck);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("操作成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String offDeck(@RequestParam("id") String storeDeckId,
                          @RequestParam("price")BigDecimal price){
        DataResult dataResult=new DataResult();
        if (!storeDeckService.isAlive(storeDeckId)){
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("装饰不存在,或装饰不处于启用状态");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreDeck storeDeck= storeDeckService.getStoreDeckById(storeDeckId);
        storeDeck.setPrice(price);
        storeDeckService.updateDeck(storeDeck);
        dataResult.setData(storeDeck);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("修改成功");
        return JsonUtil.toJsonString(dataResult);
    }

}
